// ==UserScript==
// @name			Change Menu Item Defaults For Google Analytics
// @author			Erik Vergobbi Vold (erikvvold@gmail.com)
// @datecreated		2009-08-12
// @lastupdated		2009-08-14
// @namespace		changeMenuItemDefaultsForGA
// @include			https://www.google.com/analytics/reporting/*
// @version			1.0.1
// @license			GPL version 3 or any later version; http://www.gnu.org/copyleft/gpl.html
// @description		This userscript allows you to change the default reports from the overviews to other reports.
// ==/UserScript==


var resetNavLink = function ( section_id, defaultPage, newPage ) {
	var tempNavEle = document.getElementById( section_id );

	if( tempNavEle == undefined ) {
		return false;
	}

	tempNavEle.href = (tempNavEle.href + "").replace("/"+defaultPage+"?", "/"+newPage+"?");
	tempNavEle.setAttribute("onclick", "return false;");
	tempNavEle.addEventListener("click", function(e){
		unsafeWindow.VisualizationModule.changeReport( newPage, null );
		return true;
	}, false);
	return true;
}

var changeMenuItemDefaultsForGA = function() {
	var export_btn = document.getElementById('export_button_item');

	if( !export_btn ){
		return false;
	}

	var docPage = (document.location + "").match(/\/([^\?\/]*)\?/i)[1];
	var inVistorArea = false;
	var inTrafficArea = false;
	var inContentArea = false;
	var visitorsAreas = ["visitors", "benchmark", "maps", "visitor_types", "visits", "unique_visitors", "pageviews", "average_pageviews", "time_on_site", "bounce_rate", "loyalty", "recency", "length_of_visit", "depth_of_visit", "browsers", "platforms", "os_browsers", "colors", "resolutions", "flash", "java", "networks", "hostnames", "speeds", "user_defined"];
	var trafficAreas = ["sources", "direct_sources", "referring_sources", "search_engines", "all_sources", "adwords", "keyword_position", "tv", "keywords", "campaigns", "ad_versions"];
	var contentAreas = ["content", "top_content", "content_titles", "content_drilldown", "entrances"];

	for ( var i = 0; i < visitorsAreas.length; i++ ) {
		if (docPage == visitorsAreas[i]) {
			inVistorArea = true;
			break;
		}
	}
	if (!inVistorArea) {
		for ( var i = 0; i < trafficAreas.length; i++ ) {
			if (docPage == trafficAreas[i]) {
				inTrafficArea = true;
				break;
			}
		}
		if ( !inTrafficArea ) {
			for ( var i = 0; i < contentAreas.length; i++ ) {
				if (docPage == contentAreas[i]) {
					inContentArea = true;
					break;
				}
			}
		}
	}

	if( !inVistorArea && !inTrafficArea && !inContentArea ) {
		return false;
	}

	var saveDefault = function(){
		if (inVistorArea) {
			resetNavLink( "visitors_nav_item", GM_getValue("vistorSecDefaultPage", "visitors"), docPage );
			GM_setValue("vistorSecDefaultPage", docPage);
		}
		else if (inTrafficArea){
			resetNavLink( "traffic_sources_nav_item", GM_getValue("trafficSecDefaultPage", "sources"), docPage );
			GM_setValue("trafficSecDefaultPage", docPage);
		}
		else if (inContentArea){
			resetNavLink( "content_nav_item", GM_getValue("contentSecDefaultPage", "content"), docPage );
			GM_setValue("contentSecDefaultPage", docPage);
		}
		removeMakeDefaultBtn();
		docPageIsDefault();

		return true;
	}

	var addMakeDefaultBtn = function(){
		if (content && export_btn) {
			var logo2 = document.createElement("td");
			logo2.id = "save_default_button_item";
			logo2.innerHTML = "<a href='#' class='button' id='make-default-btn'><b><b><b>Make Default</b></b><b/></a>";
			//export_btn.parentNode.insertBefore(logo2, export_btn.parentNode.firstChild);
			export_btn.parentNode.insertBefore(logo2, export_btn);

			//add event handlers
			var btn = document.getElementById("make-default-btn");
			btn.addEventListener("click", saveDefault, true);
		}
	}
	var removeMakeDefaultBtn = function(){
		var makeDefaultBtn = document.getElementById("make-default-btn");
		makeDefaultBtn.parentNode.removeChild(makeDefaultBtn);

		return;
	}

	var docPageIsDefault = function(){
		GM_addStyle("#controlbar_border_content { background-color: #FF6D06 }");
		GM_addStyle(".beta_label { color: #FFFFFF }");
		GM_addStyle("#segment_button_label { color: #FFFFFF }");

		return;
	}

	if (inVistorArea && docPage == GM_getValue("vistorSecDefaultPage", "visitors")) {
		docPageIsDefault();
	}
	else if(inTrafficArea && docPage == GM_getValue("trafficSecDefaultPage", "sources")){
		docPageIsDefault();
	}
	else if(contentAreas && docPage == GM_getValue("contentSecDefaultPage", "content")){
		docPageIsDefault();
	}
	else {
		addMakeDefaultBtn();
	}
};
changeMenuItemDefaultsForGA();

// set saved defaults
var setSavedDefaults = function(){
	var tempNavEle = document.getElementById("visitors_nav_item");

	if( tempNavEle == undefined ) {
		return false;
	}

	resetNavLink( "visitors_nav_item", "visitors", GM_getValue("vistorSecDefaultPage", "visitors") );

	resetNavLink( "traffic_sources_nav_item", "sources", GM_getValue("trafficSecDefaultPage", "sources") );

	resetNavLink( "content_nav_item", "content", GM_getValue("contentSecDefaultPage", "content") );

	return true;
}
setSavedDefaults();